<?php

include_once '../inc/db_util.php';

function viewQuiz($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    
    if ($jsArray->{'Id'} != NULL) {
        $query = mysql_query("SELECT * FROM tb_quis t 
            JOIN tb_det_matkul dm ON t.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
            WHERE dm.id_det_matkul='" . $jsArray->{'Id'} . "' ORDER BY dm.id_det_matkul, t.nama_quis");
    } else {
        $query = mysql_query("SELECT * FROM tb_quis t 
            JOIN tb_det_matkul dm ON t.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
            WHERE dm.id_det_matkul IN (
                SELECT id_det_matkul FROM tb_det_matkul WHERE nip = '" . $jsArray->{'Creator'}->{'Nip'} . "'
            ) ORDER BY dm.id_det_matkul");
    }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_quis'];
        $row_array['Nama'] = $row['nama_quis'];
        $row_array['Waktu'] = $row['waktu'];
        $row_array['Jumlah'] = $row['jumlah_soal'];
        $row_array['Awal'] = $row['tanggal_awal'];
        $row_array['Akhir'] = $row['tanggal_batas'];
        $row_array['Mulai'] = $row['jam_mulai'];
        $row_array['Selesai'] = $row['jam_selesai'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function viewQuizMhs($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    
    if ($jsArray->{'DetMatkul'}->{'Id'} != NULL) {
        $query = mysql_query("SELECT tq.*, dm.*, mm.*, IF(tn.id_quis IS NOT NULL, 1, 0) status 
			FROM tb_quis tq
            JOIN tb_det_matkul dm ON tq.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
			LEFT JOIN tb_nilai_quis tn ON tq.id_quis = tn.id_quis AND tn.id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "'
            WHERE dm.id_det_matkul='" . $jsArray->{'DetMatkul'}->{'Id'} . "' 
			ORDER BY dm.id_det_matkul, tq.nama_quis");
    } else {
        $query = mysql_query("SELECT tq.*, dm.*, mm.*, IF(tn.id_quis IS NOT NULL, 1, 0) status
			FROM tb_quis tq
            JOIN tb_det_matkul dm ON tq.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
			LEFT JOIN tb_nilai_quis tn ON tq.id_quis = tn.id_quis AND tn.id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "'
            WHERE dm.id_det_matkul IN (
                SELECT id_det_matkul FROM tb_perkuliahan WHERE id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "'
            ) ORDER BY tq.nama_quis");
    }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_quis'];
        $row_array['Nama'] = $row['nama_quis'];
        $row_array['Waktu'] = $row['waktu'];
        $row_array['Jumlah'] = $row['jumlah_soal'];
        $row_array['Awal'] = $row['tanggal_awal'];
        $row_array['Akhir'] = $row['tanggal_batas'];
        $row_array['Mulai'] = $row['jam_mulai'];
        $row_array['Selesai'] = $row['jam_selesai'];
        $row_array['Status'] = $row['status'] == 1 ? TRUE : FALSE;
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function createQuiz($json) {
    $jsArray = json_decode($json, false);

    $sql = "INSERT INTO tb_quis(id_det_matkul, nama_quis, waktu, jumlah_soal, tanggal_awal, tanggal_batas, jam_mulai, jam_selesai)
        VALUES (            
        '" . $jsArray->{'DetMatkul'}->{'Id'} . "',
        '" . $jsArray->{'Nama'} . "',
        '" . $jsArray->{'Waktu'} . "',
        '" . $jsArray->{'Jumlah'} . "',
        '" . $jsArray->{'Awal'} . "',
        '" . $jsArray->{'Akhir'} . "',
        DATE_FORMAT('" . $jsArray->{'Mulai'} . "', '%T'),
        DATE_FORMAT('" . $jsArray->{'Selesai'} . "', '%T')
        )";

    return mysql_query($sql);
}

function updateQuiz($json) {
    $jsArray = json_decode($json, false);

    $sql = "UPDATE tb_quis SET 
        nama_quis='" . $jsArray->{'Nama'} . "',
        waktu='" . $jsArray->{'Waktu'} . "', jumlah_soal='" . $jsArray->{'Jumlah'} . "',
        tanggal_awal='" . $jsArray->{'Awal'} . "', tanggal_batas='" . $jsArray->{'Akhir'} . "',
        jam_mulai=DATE_FORMAT('" . $jsArray->{'Mulai'} . "', '%T'), jam_selesai=DATE_FORMAT('" . $jsArray->{'Selesai'} . "', '%T')
        WHERE id_quis='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function deleteQuiz($json) {
    $jsArray = json_decode($json, false);

    $sql = "DELETE FROM tb_quis WHERE id_quis='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function calculateNilaiQuiz($json){
    $jsArray = json_decode($json, false);
    
    foreach ($jsArray as $value) {
       $quizId = $value->Quis->Id;
       $detMatkulId = $value->Quis->DetMatkul->Id;
       $jumlahSoal = $value->Quis->Jumlah;
       $jawab = $value->Jawaban;
       if($jawab == $value->DiJawab){
           $benar += 1;
       }
    }
    $nilai = $benar/$jumlahSoal * 100;
}

function saveNilaiQuiz($json){
    $jsArray = json_decode($json, false);
	
	$query = mysql_query("SELECT id_perkuliahan FROM tb_perkuliahan WHERE 
				id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "' AND
				id_det_matkul='" . $jsArray->{'Quis'}->{'DetMatkul'}->{'Id'} . "'");
				
	while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
		$kuliah = $row['id_perkuliahan'];
	}
	
    $sql1 = "INSERT INTO tb_nilai_quis(id_quis, id_det_matkul, id_mahasiswa, nilai)
        VALUES (            
            '" . $jsArray->{'Quis'}->{'Id'} . "',
            '" . $jsArray->{'Quis'}->{'DetMatkul'}->{'Id'} . "',
            '" . $jsArray->{'Mhs'}->{'Id'} . "',
            '" . $jsArray->{'Nilai'} . "'
        )";

	$sql2 = "UPDATE tb_perkuliahan 
            SET nilai_quis='" . $jsArray->{'Nilai'} . "' 
            WHERE id_perkuliahan = '" . $kuliah . "'";
		
	if($kuliah != NULL) {
		$retVal = mysql_query($sql1);
		if($retVal)
			return mysql_query($sql2);    
	}
	return false;
}

?>
